home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 80 / CD Actual 80 Julio-Agosto 2003.iso / Linux / LinuxGazette / lg / issue72 / misc / nielsen / sql / users.table_functions < prev   
Encoding:
Text File  |  2002-08-14  |  23.6 KB  |  719 lines

  1. drop sequence users_sequence;
  2. create sequence users_sequence;
  3. drop sequence users_sequence_backup;
  4. create sequence users_sequence_backup;
  5. drop table users;
  6. create table users (
  7. users_id int4 NOT NULL PRIMARY KEY DEFAULT nextval('users_sequence'),
  8.     date_updated  timestamp NOT NULL default CURRENT_TIMESTAMP,
  9.     date_created  timestamp NOT NULL default CURRENT_TIMESTAMP,
  10.     active int2 CHECK (active in (0,1)) DEFAULT 0,
  11. username text NOT NULL default '' ,
  12. password text NOT NULL default '' ,
  13. user_type int4 not null default 1 ,
  14. contact_id int4 not null default 0  REFERENCES contact
  15.        ON DELETE NO ACTION
  16.        ON UPDATE CASCADE 
  17. );drop table users_backup;
  18. create table users_backup (
  19. backup_id int4 NOT NULL UNIQUE DEFAULT nextval('users_sequence_backup'), 
  20.     users_id int4 NOT NULL DEFAULT 0,
  21.     date_updated  timestamp NOT NULL default CURRENT_TIMESTAMP,
  22.     date_created  timestamp NOT NULL default CURRENT_TIMESTAMP,
  23.     active int2 CHECK (active in (0,1)) DEFAULT 0,
  24.     username text NOT NULL default '',
  25. password text NOT NULL default '',
  26. user_type int4 not null default 1,
  27. contact_id int4 not null default 0, error_code text NOT NULL DEFAULT ''
  28. );
  29. drop view users_active;
  30. create view users_active as select * from users
  31.         where active = 1;
  32. drop view users_deleted;
  33. create view users_deleted as select * from users
  34.         where active = 0;
  35. drop view users_backup_ids;
  36. create view users_backup_ids as 
  37.            select distinct users_id from users_backup;
  38. drop view users_purged;
  39. create view users_purged as
  40.    select * from users_backup where oid = ANY (
  41.      select max(oid) from users_backup where users_id = ANY
  42.         (
  43.         select distinct users_id from users_backup
  44.           where users_backup.error_code = 'purge'
  45.            and NOT users_id = ANY (select users_id from users)
  46.         )
  47.         group by users_id
  48.      )
  49.     ;
  50. ---              Generic Functions for Perl/Postgresql version 1.0
  51.  
  52. ---                       Copyright 2001, Mark Nielsen
  53. ---                            All rights reserved.
  54. ---    This Copyright notice was copied and modified from the Perl 
  55. ---    Copyright notice. 
  56. ---    This program is free software; you can redistribute it and/or modify
  57. ---    it under the terms of either:
  58.  
  59. ---        a) the GNU General Public License as published by the Free
  60. ---        Software Foundation; either version 1, or (at your option) any
  61. ---        later version, or
  62.  
  63. ---        b) the "Artistic License" which comes with this Kit.
  64.  
  65. ---    This program is distributed in the hope that it will be useful,
  66. ---    but WITHOUT ANY WARRANTY; without even the implied warranty of
  67. ---    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See either
  68. ---    the GNU General Public License or the Artistic License for more details.
  69.  
  70. ---    You should have received a copy of the Artistic License with this
  71. ---    Kit, in the file named "Artistic".  If not, I'll be glad to provide one.
  72.  
  73. ---    You should also have received a copy of the GNU General Public License
  74. ---   along with this program in the file named "Copying". If not, write to the 
  75. ---   Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 
  76. ---    02111-1307, USA or visit their web page on the internet at
  77. ---    http://www.gnu.org/copyleft/gpl.html.
  78.  
  79. -- create a method to unpurge just one item.  
  80. -- create a method to purge one item. 
  81. --  \i /tmp/Test/sample/users.table
  82. ---------------------------------------------------------------------
  83.  
  84. drop function sql_users_insert ();
  85. CREATE FUNCTION sql_users_insert () RETURNS int4 AS '
  86. DECLARE
  87.     record1 record;  oid1 int4; id int4 :=0; record_backup RECORD;
  88. BEGIN
  89.    insert into users (date_updated, date_created, active)
  90.         values (CURRENT_TIMESTAMP,CURRENT_TIMESTAMP, 1);
  91.      -- Get the unique oid of the row just inserted. 
  92.    GET DIAGNOSTICS oid1 = RESULT_OID;
  93.      -- Get the users id. 
  94.    FOR record1 IN SELECT users_id FROM users where oid = oid1
  95.       LOOP
  96.       id := record1.users_id;
  97.    END LOOP;
  98.    
  99.      -- If id is NULL, insert failed or something is wrong.
  100.    IF id is NULL THEN return (-1); END IF;
  101.      -- It should also be greater than 0, otherwise something is wrong.
  102.    IF id < 1 THEN return (-2); END IF;
  103.  
  104.       -- Now backup the data. 
  105.     FOR record_backup IN SELECT * FROM users where users_id = id
  106.        LOOP
  107.        insert into users_backup (users_id, date_updated, date_created, 
  108.            active, error_code) 
  109.          values (id, record_backup.date_updated, record_backup.date_created,
  110.             record_backup.active, ''insert'');
  111.     END LOOP;
  112.  
  113.      -- Everything has passed, return id as users_id.
  114.    return (id);
  115. END;
  116. ' LANGUAGE 'plpgsql';
  117. ---------------------------------------------------------------------
  118.  
  119. drop function sql_users_delete (int4);
  120. CREATE FUNCTION sql_users_delete (int4) RETURNS int2 AS '
  121. DECLARE
  122.     id int4 := 0;
  123.     id_exists int4 := 0;
  124.     record1 RECORD; 
  125.     record_backup RECORD;
  126.     return_int4 int4 :=0;
  127.  
  128. BEGIN
  129.      -- If the id is not greater than 0, return error.
  130.    id := clean_numeric($1);
  131.    IF id < 1 THEN return -1; END IF;
  132.  
  133.      -- If we find the id, set active = 0. 
  134.    FOR record1 IN SELECT users_id FROM users 
  135.           where users_id = id
  136.       LOOP
  137.       update users set active=0, date_updated = CURRENT_TIMESTAMP
  138.            where users_id = id;  
  139.       GET DIAGNOSTICS return_int4 = ROW_COUNT;       
  140.       id_exists := 1;
  141.    END LOOP;
  142.       
  143.      -- If we did not find the id, abort and return -2.  
  144.    IF id_exists = 0 THEN return (-2); END IF;
  145.  
  146.    FOR record_backup IN SELECT * FROM users where users_id = id
  147.       LOOP
  148.       insert into users_backup (users_id, date_updated, date_created,
  149.           active , username, password, user_type, contact_id ,error_code)
  150.            values (record_backup.users_id, record_backup.date_updated, 
  151.              record_backup.date_updated, record_backup.active
  152.              , record_backup.username, record_backup.password, record_backup.user_type, record_backup.contact_id , ''delete''
  153.       );
  154.    END LOOP;
  155.  
  156.      -- If id_exists == 0, Return error.
  157.      -- It means it never existed. 
  158.    IF id_exists = 0 THEN return (-1); END IF;
  159.  
  160.      -- We got this far, it must be true, return ROW_COUNT.   
  161.    return (return_int4);
  162. END;
  163. ' LANGUAGE 'plpgsql';
  164.  
  165. ---------------------------------------------------------------------
  166.  
  167. drop function sql_users_undelete (int4);
  168. CREATE FUNCTION sql_users_undelete (int4) RETURNS int2 AS '
  169. DECLARE
  170.     id int4 := 0;
  171.     id_exists int4 := 0;
  172.     record1 RECORD; 
  173.     record_backup RECORD;
  174.     return_int4 int4 :=0;
  175.  
  176. BEGIN
  177.      -- If the id is not greater than 0, return error.
  178.    id := clean_numeric($1);
  179.    IF id < 1 THEN return -1; END IF;
  180.  
  181.      -- If we find the id, set active = 1. 
  182.    FOR record1 IN SELECT users_id FROM users 
  183.           where users_id = id
  184.       LOOP
  185.       update users set active=1, date_updated = CURRENT_TIMESTAMP
  186.            where users_id = id;  
  187.       GET DIAGNOSTICS return_int4 = ROW_COUNT;       
  188.       id_exists := 1;
  189.    END LOOP;
  190.       
  191.      -- If we did not find the id, abort and return -2.  
  192.    IF id_exists = 0 THEN return (-2); END IF;
  193.  
  194.    FOR record_backup IN SELECT * FROM users where users_id = id
  195.       LOOP
  196.       insert into users_backup (users_id, date_updated, date_created,
  197.           active , username, password, user_type, contact_id ,error_code)
  198.            values (record_backup.users_id, record_backup.date_updated, 
  199.              record_backup.date_updated, record_backup.active
  200.              , record_backup.username, record_backup.password, record_backup.user_type, record_backup.contact_id , ''undelete''
  201.       );
  202.    END LOOP;
  203.  
  204.      -- If id_exists == 0, Return error.
  205.      -- It means it never existed. 
  206.    IF id_exists = 0 THEN return (-1); END IF;
  207.  
  208.      -- We got this far, it must be true, return ROW_COUNT.   
  209.    return (return_int4);
  210. END;
  211. ' LANGUAGE 'plpgsql';
  212.  
  213. ---------------------------------------------------------------------
  214. drop function sql_users_update (int4 , text, text, int4, int4);
  215. CREATE FUNCTION sql_users_update  (int4 , text, text, int4, int4) 
  216.   RETURNS int2 AS '
  217. DECLARE
  218.     id int4 := 0;
  219.     id_exists int4 := 0;
  220.     record_update RECORD; record_backup RECORD;
  221.     return_int4 int4 :=0;
  222.               var_2 text;
  223.           var_3 text;
  224.           var_4 int4;
  225.           var_5 int4;
  226.  
  227. BEGIN
  228.              var_2 := clean_text($2);
  229.          var_3 := clean_text($3);
  230.          var_4 := clean_numeric($4);
  231.          var_5 := clean_numeric($5);
  232.  
  233.      -- If the id is not greater than 0, return error.
  234.    id := clean_numeric($1);
  235.    IF id < 1 THEN return -1; END IF;
  236.  
  237.    FOR record_update IN SELECT users_id FROM users
  238.          where users_id = id
  239.       LOOP
  240.       id_exists := 1;
  241.    END LOOP;
  242.  
  243.    IF id_exists = 0 THEN return (-2); END IF;
  244.  
  245.    update users set date_updated = CURRENT_TIMESTAMP
  246.       , username = var_2, password = var_3, user_type = var_4, contact_id = var_5 
  247.         where users_id = id;
  248.    GET DIAGNOSTICS return_int4 = ROW_COUNT;
  249.  
  250.    FOR record_backup IN SELECT * FROM users where users_id = id
  251.       LOOP
  252.      insert into users_backup (users_id,
  253.          date_updated, date_created, active
  254.          , username, password, user_type, contact_id, error_code)
  255.        values (record_update.users_id, record_backup.date_updated,
  256.          record_backup.date_updated, record_backup.active
  257.          , record_backup.username, record_backup.password, record_backup.user_type, record_backup.contact_id, ''update''
  258.       );
  259.    END LOOP;
  260.  
  261.      -- We got this far, it must be true, return ROW_COUNT.   
  262.    return (return_int4);
  263. END;
  264. ' LANGUAGE 'plpgsql';
  265. ---------------------------------------------------------------------
  266.  
  267. drop function sql_users_copy (int4);
  268. CREATE FUNCTION sql_users_copy (int4) 
  269.   RETURNS int2 AS '
  270. DECLARE
  271.     id int4 := 0;
  272.     id_exists int4 := 0;
  273.     record1 RECORD; record2 RECORD; record3 RECORD;    
  274.     return_int4 int4 := 0;
  275.     id_new int4 := 0;
  276.     users_new int4 :=0;
  277. BEGIN
  278.      -- If the id is not greater than 0, return error.
  279.    id := clean_numeric($1);
  280.    IF id < 1 THEN return -1; END IF;
  281.  
  282.    FOR record1 IN SELECT users_id FROM users where users_id = id
  283.       LOOP
  284.       id_exists := 1;
  285.    END LOOP;
  286.    IF id_exists = 0 THEN return (-2); END IF;
  287.  
  288.      --- Get the new id
  289.    FOR record1 IN SELECT sql_users_insert() as users_insert
  290.       LOOP
  291.       users_new := record1.users_insert;
  292.    END LOOP;
  293.      -- If the users_new is not greater than 0, return error.
  294.    IF users_new < 1 THEN return -3; END IF;
  295.  
  296.    FOR record2 IN SELECT * FROM users where users_id = id
  297.       LOOP
  298.  
  299.      FOR record1 IN SELECT sql_users_update(users_new , clean_text(record2.username), clean_text(record2.password), clean_text(record2.user_type), clean_text(record2.contact_id))
  300.         as users_insert
  301.       LOOP
  302.         -- execute some arbitrary command just to get it to pass. 
  303.       id_exists := 1;
  304.      END LOOP;
  305.    END LOOP;
  306.  
  307.      -- We got this far, it must be true, return new id.   
  308.    return (users_new);
  309. END;
  310. ' LANGUAGE 'plpgsql';
  311.  
  312. ------------------------------------------------------------------
  313. drop function sql_users_purge ();
  314. CREATE FUNCTION sql_users_purge () RETURNS int4 AS '
  315. DECLARE
  316.     record_backup RECORD; oid1 int4 := 0;
  317.     return_int4 int4 :=0;
  318.     deleted int4 := 0;
  319.     delete_count int4 :=0;
  320.     delete_id int4;
  321.  
  322. BEGIN 
  323.  
  324.      -- Now delete one by one. 
  325.    FOR record_backup IN SELECT * FROM users where active = 0
  326.       LOOP
  327.          -- Record the id we want to delete. 
  328.       delete_id = record_backup.users_id;
  329.  
  330.       insert into users_backup (users_id, date_updated, date_created,
  331.           active , username, password, user_type, contact_id ,error_code)
  332.            values (record_backup.users_id, record_backup.date_updated, 
  333.              record_backup.date_updated, record_backup.active
  334.              , record_backup.username, record_backup.password, record_backup.user_type, record_backup.contact_id , ''purge''
  335.           );
  336.  
  337.         -- Get the unique oid of the row just inserted. 
  338.       GET DIAGNOSTICS oid1 = RESULT_OID;
  339.  
  340.         -- If oid1 less than 1, return -1
  341.       IF oid1 < 1 THEN return (-2); END IF;
  342.         -- Now delete this from the main table.   
  343.       delete from users where users_id = delete_id;
  344.  
  345.         -- Get row count of row just deleted, should be 1. 
  346.       GET DIAGNOSTICS deleted = ROW_COUNT;
  347.         -- If deleted less than 1, return -3
  348.       IF deleted < 1 THEN return (-3); END IF;
  349.       delete_count := delete_count + 1;
  350.  
  351.     END LOOP;
  352.  
  353.      -- We got this far, it must be true, return the number of ones we had.  
  354.    return (delete_count);
  355. END;
  356. ' LANGUAGE 'plpgsql';
  357.  
  358. ------------------------------------------------------------------
  359. drop function sql_users_purgeone (int4);
  360. CREATE FUNCTION sql_users_purgeone (int4) RETURNS int4 AS '
  361. DECLARE
  362.     record_backup RECORD; oid1 int4 := 0;
  363.     record1 RECORD;
  364.     return_int4 int4 :=0;
  365.     deleted int4 := 0;
  366.     delete_count int4 :=0;
  367.     delete_id int4;
  368.     purged_no int4 := 0;
  369.  
  370. BEGIN
  371.  
  372.     delete_id := $1;
  373.         -- If purged_id less than 1, return -4
  374.     IF delete_id < 1 THEN return (-4); END IF;
  375.  
  376.    FOR record1 IN SELECT * FROM users 
  377.       where active = 0 and users_id = delete_id 
  378.       LOOP
  379.       purged_no := purged_no + 1;
  380.    END LOOP;
  381.  
  382.         -- If purged_no less than 1, return -1
  383.    IF purged_no < 1 THEN return (-1); END IF;
  384.  
  385.      -- Now delete one by one.
  386.    FOR record_backup IN SELECT * FROM users where users_id = delete_id
  387.       LOOP
  388.  
  389.       insert into users_backup (users_id, date_updated, date_created,
  390.           active , username, password, user_type, contact_id ,error_code)
  391.            values (record_backup.users_id, record_backup.date_updated,
  392.              record_backup.date_updated, record_backup.active
  393.              , record_backup.username, record_backup.password, record_backup.user_type, record_backup.contact_id , ''purgeone''
  394.           );
  395.  
  396.         -- Get the unique oid of the row just inserted.
  397.       GET DIAGNOSTICS oid1 = RESULT_OID;
  398.  
  399.         -- If oid1 less than 1, return -2
  400.       IF oid1 < 1 THEN return (-2); END IF;
  401.         -- Now delete this from the main table.
  402.       delete from users where users_id = delete_id;
  403.  
  404.         -- Get row count of row just deleted, should be 1.
  405.       GET DIAGNOSTICS deleted = ROW_COUNT;
  406.         -- If deleted less than 1, return -3
  407.       IF deleted < 1 THEN return (-3); END IF;
  408.       delete_count := delete_count + 1;
  409.  
  410.     END LOOP;
  411.  
  412.      -- We got this far, it must be true, return the number of ones we had.
  413.    return (delete_count);
  414. END;
  415. ' LANGUAGE 'plpgsql';
  416.  
  417. ------------------------------------------------------------------------
  418. drop function sql_users_unpurge ();
  419. CREATE FUNCTION sql_users_unpurge () RETURNS int2 AS '
  420. DECLARE
  421.     record1 RECORD;
  422.     record2 RECORD; 
  423.     record_backup RECORD;
  424.     purged_id int4 := 0;
  425.     purge_count int4 :=0;
  426.     timestamp1 timestamp;
  427.     purged_no int4 := 0;
  428.     oid1 int4 := 0;
  429.     oid_found int4 := 0;
  430.     highest_oid int4 := 0;
  431.  
  432. BEGIN
  433.  
  434.      -- Now get the unique ids that were purged. 
  435.    FOR record1 IN select distinct users_id from users_backup 
  436.        where users_backup.error_code = ''purge''
  437.           and NOT users_id = ANY (select users_id from users)
  438.       LOOP
  439.  
  440.       purged_id := record1.users_id;
  441.       timestamp1 := CURRENT_TIMESTAMP;
  442.       purged_no := purged_no + 1;
  443.       oid_found := 0;
  444.       highest_oid := 0;
  445.  
  446.         -- Now we have the unique id, find its latest date. 
  447.  
  448.       FOR record2 IN select max(oid) from users_backup 
  449.           where users_id = purged_id and error_code = ''purge''
  450.         LOOP 
  451.           -- record we got the date and also record the highest date.
  452.         oid_found := 1; 
  453.         highest_oid := record2.max;
  454.       END LOOP;
  455.  
  456.          -- If the oid_found is 0, return error. 
  457.       IF oid_found = 0 THEN return (-3); END IF;
  458.  
  459.         -- Now we have the latest date, get the values and insert them. 
  460.       FOR record_backup IN select * from users_backup 
  461.           where oid = highest_oid
  462.         LOOP 
  463.  
  464.       insert into users_backup (users_id, date_updated, date_created,
  465.           active , username, password, user_type, contact_id ,error_code)
  466.            values (purged_id, record_backup.date_updated, 
  467.              timestamp1, record_backup.active
  468.              , record_backup.username, record_backup.password, record_backup.user_type, record_backup.contact_id , ''unpurge''
  469.           );
  470.  
  471.         -- Get the unique oid of the row just inserted. 
  472.       GET DIAGNOSTICS oid1 = RESULT_OID;
  473.         -- If oid1 less than 1, return -1
  474.       IF oid1 < 1 THEN return (-1); END IF;
  475.  
  476.       insert into users (users_id, date_updated, date_created,
  477.           active , username, password, user_type, contact_id)
  478.            values (purged_id, timestamp1,
  479.              timestamp1, record_backup.active
  480.              , record_backup.username, record_backup.password, record_backup.user_type, record_backup.contact_id );
  481.         -- Get the unique oid of the row just inserted.
  482.       GET DIAGNOSTICS oid1 = RESULT_OID;
  483.         -- If oid1 less than 1, return -2
  484.       IF oid1 < 1 THEN return (-2); END IF;
  485.  
  486.       END LOOP;
  487.  
  488.    END LOOP;
  489.  
  490.      -- We got this far, it must be true, return how many were affected.  
  491.    return (purged_no);
  492. END;
  493. ' LANGUAGE 'plpgsql';
  494.  
  495. ---------------------------------------------------------------------
  496. drop function sql_users_unpurgeone (int4);
  497. CREATE FUNCTION sql_users_unpurgeone (int4) RETURNS int2 AS '
  498. DECLARE
  499.     record_id int4;
  500.     record1 RECORD;
  501.     record2 RECORD;
  502.     record_backup RECORD;
  503.     return_int4 int4 :=0;
  504.     purged_id int4 := 0;
  505.     purge_count int4 :=0;
  506.     timestamp1 timestamp;
  507.     purged_no int4 := 0;
  508.     oid1 int4 := 0;
  509.     oid_found int4 := 0;
  510.     highest_oid int4 := 0;
  511.  
  512. BEGIN
  513.  
  514.       purged_id := $1;
  515.         -- If purged_id less than 1, return -1
  516.       IF purged_id < 1 THEN return (-1); END IF;
  517.         --- Get the current timestamp.
  518.       timestamp1 := CURRENT_TIMESTAMP;
  519.  
  520.    FOR record1 IN select distinct users_id from users_backup
  521.        where users_backup.error_code = ''purge''
  522.           and NOT users_id = ANY (select users_id from users)
  523.           and users_id = purged_id
  524.       LOOP
  525.       purged_no := purged_no + 1;
  526.  
  527.    END LOOP;
  528.  
  529.         -- If purged_no less than 1, return -1
  530.    IF purged_no < 1 THEN return (-3); END IF;
  531.  
  532.         -- Now find the highest oid.  
  533.    FOR record2 IN select max(oid) from users_backup
  534.           where users_id = purged_id and error_code = ''purge''
  535.         LOOP
  536.           -- record we got the date and also record the highest date.
  537.         oid_found := 1;
  538.         highest_oid := record2.max;
  539.     END LOOP;
  540.  
  541.          -- If the oid_found is 0, return error.
  542.     IF oid_found = 0 THEN return (-4); END IF;
  543.  
  544.         -- Now get the data and restore it. 
  545.     FOR record_backup IN select * from users_backup 
  546.           where oid  = highest_oid
  547.         LOOP 
  548.         -- Insert into backup that it was unpurged. 
  549.       insert into users_backup (users_id, date_updated, date_created,
  550.           active , username, password, user_type, contact_id ,error_code)
  551.            values (purged_id, timestamp1, 
  552.              record_backup.date_created, record_backup.active
  553.              , record_backup.username, record_backup.password, record_backup.user_type, record_backup.contact_id , ''unpurgeone''
  554.           );
  555.  
  556.         -- Get the unique oid of the row just inserted. 
  557.       GET DIAGNOSTICS oid1 = RESULT_OID;
  558.         -- If oid1 less than 1, return -1
  559.       IF oid1 < 1 THEN return (-1); END IF;
  560.         -- Insert into live table. 
  561.       insert into users (users_id, date_updated, date_created,
  562.           active , username, password, user_type, contact_id)
  563.            values (record_backup.users_id, timestamp1,
  564.              record_backup.date_updated, record_backup.active
  565.              , record_backup.username, record_backup.password, record_backup.user_type, record_backup.contact_id );
  566.         -- Get the unique oid of the row just inserted.
  567.       GET DIAGNOSTICS oid1 = RESULT_OID;
  568.         -- If oid1 less than 1, return -2
  569.       IF oid1 < 1 THEN return (-2); END IF;
  570.  
  571.       END LOOP;
  572.  
  573.      -- We got this far, it must be true, return how many were affected (1).  
  574.    return (purged_no);
  575. END;
  576. ' LANGUAGE 'plpgsql';
  577.  
  578. insert into users (users_id, date_updated, date_created, active)
  579.     values (0, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 0);
  580. insert into users_backup (backup_id, users_id, 
  581.      date_updated, date_created, active, error_code)
  582.     values (0, 0, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP, 0, 'table creation');
  583.  
  584.  
  585.  
  586.  
  587. drop function clean_text (text);
  588. CREATE FUNCTION  clean_text (text) RETURNS text AS '
  589.   my $Text = shift;
  590.     # Get rid of whitespace in front. 
  591.   $Text =~ s/^\\s+//;
  592.     # Get rid of whitespace at end. 
  593.   $Text =~ s/\\s+$//;
  594.     # Get rid of anything not text.
  595.   $Text =~ s/[^ a-z0-9\\/\\`\\~\\!\\@\\#\\$\\%\\^\\&\\*\\(\\)\\-\\_\\=\\+\\\\\\|\[\\{\\]\\}\\;\\:\\''\\"\\,\\<\\.\\>\\?\\t\\n]//gi;
  596.     # Replace all multiple whitespace with one space. 
  597.   $Text =~ s/\\s+/ /g;
  598.   return $Text;
  599. ' LANGUAGE 'plperl';
  600.  -- Just do show you what this function cleans up. 
  601. select clean_text ('       ,./<>?aaa aa      !@#$%^&*()_+| ');
  602.  
  603. drop function clean_alpha (text);
  604. CREATE FUNCTION  clean_alpha (text) RETURNS text AS '
  605.   my $Text = shift;
  606.   $Text =~ s/[^a-z0-9_]//gi;
  607.   return $Text;
  608. ' LANGUAGE 'plperl';
  609.  -- Just do show you what this function cleans up. 
  610. select clean_alpha ('       ,./<>?aaa aa      !@#$%^&*()_+| ');
  611.  
  612. drop function clean_numeric (text);
  613. CREATE FUNCTION  clean_numeric (text) RETURNS int4 AS '
  614.   my $Text = shift;
  615.   $Text =~ s/[^0-9]//gi;
  616.   return $Text;
  617. ' LANGUAGE 'plperl';
  618.  -- Just do show you what this function cleans up.
  619. select clean_numeric ('       ,./<>?aaa aa      !@#$%^&*()_+| ');
  620.  
  621. drop function clean_numeric (int4);
  622. CREATE FUNCTION  clean_numeric (int4) RETURNS int4 AS '
  623.   my $Text = shift;
  624.   $Text =~ s/[^0-9]//gi;
  625.   return $Text;
  626. ' LANGUAGE 'plperl';
  627.  -- Just do show you what this function cleans up.
  628. select clean_numeric (11111);
  629.  
  630.  
  631.  
  632. select sql_users_insert();
  633. select sql_users_update(1,'Username1','Password1',1);
  634. select sql_users_insert();
  635. select sql_users_update(2,'Username2','Password2',1);
  636.  
  637. drop function sql_users_verify(text,text);
  638. CREATE FUNCTION sql_users_verify (text,text) RETURNS int4 AS '
  639. DECLARE
  640.     record1 record;  id int4 :=0;
  641.     username text; password text;
  642.     active int4 := 0;
  643. BEGIN
  644.    username := clean_text($1);
  645.    password := clean_text($2);
  646.  
  647.    FOR record1 IN SELECT users_id FROM users
  648.         where users.username = username
  649.           and users.password = password
  650.           and users.active = 1
  651.       LOOP
  652.       id := record1.users_id;
  653.    END LOOP;
  654.  
  655.      -- If id < 1 check if username exists .
  656.    IF id is NULL THEN  
  657.      FOR record1 IN SELECT users_id,active FROM users
  658.         where users.username = username
  659.        LOOP
  660.        id := record1.users_id;
  661. --       active := record1.active;
  662.          -- If active is < 1, isn not active.
  663. --       IF active < 1 THEN return (-3); END IF;
  664.          -- If id is > 0, password is incorrect.
  665.        IF id  > 0 THEN return (-2); END IF;
  666.        END LOOP;
  667.    END IF;
  668.  
  669.         -- If id is < 1, username does not exist. 
  670.    IF id  < 1 THEN return (-1); END IF;
  671.  
  672.      -- Everything has passed, return id as users_id.
  673.    return (id);
  674. END;
  675. ' LANGUAGE 'plpgsql';
  676. select sql_users_verify('Username1','Password1');
  677.  
  678. drop function sql_users_exists(text);
  679. CREATE FUNCTION sql_users_exists (text) RETURNS int4 AS '
  680. DECLARE
  681.     record1 record;  id int4 :=0;
  682.     username text; password text;
  683. BEGIN
  684.    username := clean_text($1);
  685.  
  686.    FOR record1 IN SELECT users_id FROM users
  687.         where users.username = username
  688.       LOOP
  689.       id := record1.users_id;
  690.    END LOOP;
  691.  
  692.      -- Everything has passed, return id as users_id.
  693.    return (id);
  694. END;
  695. ' LANGUAGE 'plpgsql';
  696. select sql_users_exists('Username1');
  697.  
  698. drop function sql_users_active(text);
  699. CREATE FUNCTION sql_users_active (text) RETURNS int4 AS '
  700. DECLARE
  701.     record1 record;  id int4 :=0;
  702.     username text; password text;
  703. BEGIN
  704.    username := clean_text($1);
  705.  
  706.    FOR record1 IN SELECT users_id FROM users where users.username = username
  707.       LOOP
  708.       id := record1.users_id;
  709.    END LOOP;
  710.  
  711.      -- Everything has passed, return id as users_id.
  712.    return (id);
  713. END;
  714. ' LANGUAGE 'plpgsql';
  715. select sql_users_active('Username1');
  716.  
  717.  
  718. vacuum;
  719.